﻿using UnityEngine;
using System.Collections;
public enum BoundingBoxType{
	Cube,
	Sphere,
}
public interface IBoundingBox{
	Vector3 GetRandomPosition();
}
public class BoundingBox : MonoBehaviour,IBoundingBox {
	[SerializeField]BoundingBoxType boundingBoxType;
	[SerializeField]Vector3 offset = Vector3.zero;

	[SerializeField]Vector3 size = Vector3.one;
	[SerializeField]float radius = 1;

	public delegate Vector3 V3Delegate();
	V3Delegate getRandomPosition;

	void Start(){
		switch (boundingBoxType) {
		case BoundingBoxType.Cube:
			getRandomPosition = GetPositionByCube;
			break;
		case BoundingBoxType.Sphere:
			getRandomPosition = GetPositionBySphere;
			break;
		}

	}

	public Vector3 GetRandomPosition(){
		if (getRandomPosition != null)
			return getRandomPosition ();
		else
			return transform.position;
	}

	Vector3 GetPositionByCube(){
		Vector3 pos = transform.position + offset;
		Vector3 halfsize = size * 0.5f;
		pos.x += Random.Range (-halfsize.x, halfsize.x);
		pos.y += Random.Range (-halfsize.y, halfsize.y);
		pos.z += Random.Range (-halfsize.z, halfsize.z);
		return pos;
	}
	Vector3 GetPositionBySphere(){
		Vector3 pos = transform.position + offset;
		pos.x += Random.Range (-radius, radius);
		pos.y += Random.Range (-radius, radius);
		pos.z += Random.Range (-radius, radius);
		return pos;
	}

	public void OnDrawGizmos(){
		Gizmos.color = Color.yellow;
		if(boundingBoxType == BoundingBoxType.Cube)
			Gizmos.DrawWireCube (transform.position + offset, size);
		else
			Gizmos.DrawWireSphere (transform.position + offset, radius);
		Gizmos.color = Color.white;
	}
}
